(function () {

	'use strict';

	angular
		.module('app')
		.controller('ConfirmController', ConfirmController);

	ConfirmController.$inject = ['$scope', '$state', '$rootScope', '$stateParams', 'API', 'ConfirmProductService', 'ExpressSelectService', 'InvoiceSelectService', 'PaymentModel'];

	function ConfirmController($scope, $state, $rootScope, $stateParams, API, ConfirmProductService, ExpressSelectService, InvoiceSelectService, PaymentModel) {

        $scope.touchAddress = function() {
            $state.go('address-select', {
                address_id: $scope.address ? $scope.address.address_id : null
            });
        };

        $scope.touchProduct = function(part_id) {
            $state.go('pre-sales-info', {
                part_id: part_id
            });
        };

        $scope.touchInvoice = function() {
            $scope.toast('暂不支持在线开票，请联系客服开具发票');
        };

        $scope.touchExpress = function () {
            if (!$scope.address) {
                $scope.toast('请选择地址');
                return;
            }

            var goodsIds = [];
            var numbers = [];
            goodsIds.push($scope.selectedGoods.goods.goods_id);
            numbers.push($scope.selectedGoods.amount);

            ExpressSelectService.clear();
            ExpressSelectService.expressId = $scope.express ? $scope.express.express_id : null;
            ExpressSelectService.goodsIds = goodsIds;
            ExpressSelectService.goodsNumbers = numbers;
            ExpressSelectService.address_id = $scope.address.address_id;

            $state.go('express-select', {});
        };

        $scope.touchSubmit = _touchSubmit;

		$scope.refreshComment = function (){
            ConfirmProductService.input.comment = $scope.input.comment;
        };

		$scope.addressList = [];
		$scope.goods = ConfirmProductService.goods;
		$scope.address = ConfirmProductService.address;
		$scope.express = ConfirmProductService.express;
		$scope.all_discount = 0;
		$scope.input = {
			comment: ""
		};

		$scope.input.comment = ConfirmProductService.input.comment;

		$scope.priceInfo = {};

		$scope.canPurchase = function () {
            if (!$scope.selectedGoods)
                return false;
            if (!$scope.address)
                return false;
            if (!$scope.express)
                return false;

            return true;
        };

		$scope.selectedGoods = {};
		if(ConfirmProductService.goods){
            $scope.selectedGoods.goods = ConfirmProductService.goods;
            $scope.selectedGoods.amount = 1;//默认购买一份
		} else {
			return;
		}

        _reloadAddress();
		_refreshOrderPrice();

        function _reloadAddress() {
            API.consignee.getAddList({}).then(function (res) {
                if (res) {
                    $scope.addList = res;
                    if (!$scope.address) {
                        for (var key in $scope.addList) {
                            if ($scope.addList[key].is_default) {
                                $scope.address = $scope.addList[key];
                                $rootScope.$emit('consigneeChanged', $scope.address);
                            }
                        }
                    }
                }
                _reloadExpress();
            })
        }

        function _reloadExpress() {
            if (!$scope.express && $scope.selectedGoods.goods.is_real==1) {
                var goods = {};
                goods.goods_id = $scope.selectedGoods.goods.goods_id;
                goods.num = $scope.selectedGoods.amount;

                API.shipping.getFirstExpress({
                    goods: JSON.stringify([goods]),
                    address_id: $scope.address.address_id
                }).then(function (res) {
                    $scope.express = res;
                    _refreshOrderPrice();
                });
            }
        }

		function _refreshOrderPrice() {
            var goods = $scope.goods;
            var express = $scope.express;

            if (!goods) {
                $scope.toast('商品信息不存在');
                return;
            }

            var products = {};
            products.goods_id = goods.goods_id;
            products.num = 1;

            var params = {};
            params.order_goods = JSON.stringify([products]);
            if (express) {
                params.express_region_id = express.express_region_id;
            }

            API.order.getCheckPrice(params).then(function (res) {
                $scope.priceInfo = res;
                $scope.all_discount = res.discount_price;
            });
		}

		function _touchSubmit() {
            var goods = $scope.goods;
            var address = $scope.address;
            var express = $scope.express;
            var comment = $scope.input.comment;

            if (!goods) {
                $scope.toast('商品信息不存在');
                return;
            }

            if (!address) {
                $scope.toast('请填写地址');
                return;
            }

            if (!$scope.express && goods.is_real==1) {
                $scope.toast('请选择快递');
                return;
            }

            var params = {
                goods_id: goods.goods_id,
                address_id: address ? address.address_id : null,
                express_region_id: express ? express.express_region_id : null,
                leave_words: comment
            };
            API.cart.purchase(params).then(function (res) {
                if (res) {
                    ConfirmProductService.clear();
                    ExpressSelectService.clear();
                    PaymentModel.clear();
                    $state.go('payment', {
                        order_id: res.order_id
                    });
                }
            });
		}

	}

})();